/**************************************************************************
 * @doc User Interface
 * @module PASSWD.C |  
 * Source code for handling password access.
 *
 * Product     :   SoftPay 2000 (Global Payment Methodology).
 * <nl>Developer   :   SoftPay Development Team (I_SP2000).
 * <nl>Notes       :
 *
 * @head3 UI Functions |
 * @index | UI
 * @end
 *
 * Copyright (c) 1996-2000 by VeriFone Inc. All rights reserved.
 * 
 * No part of this software may be used, stored, compiled, reproduced,
 * modified, transcribed, translated, transmitted, or transferred, in any form 
 * or by any means whether electronic, mechanical, magnetic, optical, 
 * or otherwise, without the express prior written permission of VeriFone, Inc.
 *
 * Revision History
 * ------------------------------------------------------------------------
 $Log: /SoftPay/SPK2SIM/Source/Ui/passwd.c $
 ********************************************************************************/
#include <project.h>
#define VSPASSWD_C

#include <aclconio.h>
#include <applidl.h>
#include <string.h>

#ifdef DEBUG_VSPASSWD
#define DEBUG_95
#endif

#include <define.h>
#include <transact.h>
#include <passwd.h>        
#include <gds.h>
#include <menufunc.h>
#include <menus.h>
#include <date.h>
#include <ui.h>
#include <scalls.h>
#include <msg.h>
#include <msgfuncs.h>
#include <spamsg.h>   
#include <tct.h>
#include <trt.h>
#include <spdebug.h>
#include <tran.h>
#include <funcids.h>
#include <table.h>
#include <udt.h>
#include <tblutil.h>
#include <confio.h>
#include <entry.h>
#include <codesec.h>
#include <pit.h>

static  int inCurrentPWSecurityLevel = ACCESS_FREELY;

void vdSetPWSecurityLevel(int inPasswordLevel) {
    inCurrentPWSecurityLevel = inPasswordLevel;
}

int inGetPWSecurityLevel(void) {
    return (inCurrentPWSecurityLevel);
}

int inCheckPWSecurityLevel(int inPasswordLevel) {
    int inRetVal = VS_ERROR;

    switch (inPasswordLevel) {
    case ACCESS_WITH_MANAGER_PASSWORD:
        if (inCurrentPWSecurityLevel == ACCESS_WITH_MANAGER_PASSWORD)
            inRetVal = VS_SUCCESS;
        break;

    case ACCESS_WITH_SUPER_PASSWORD:
        if (inCurrentPWSecurityLevel == ACCESS_WITH_SUPER_PASSWORD || inCurrentPWSecurityLevel == ACCESS_WITH_MANAGER_PASSWORD)
            inRetVal = VS_SUCCESS;
        break;

    case ACCESS_WITH_CLERK_PASSWORD:
        if (inCurrentPWSecurityLevel != ACCESS_FREELY)
            inRetVal = VS_SUCCESS;
        break;

    case ACCESS_WITH_CUSTOM:
        if (inCurrentPWSecurityLevel == ACCESS_WITH_CUSTOM)
            inRetVal = VS_SUCCESS;
        break;

    case ACCESS_FREELY:
    default:
        inRetVal = VS_SUCCESS;
        break;
    }

    return inRetVal;
}

/********************************************************************************************* 
* @func int | inGetPassword |
*             Prompts for the password and saves the result 
*
* @parm long | lnMessage | 
*       Message to diplay at prompt.
*
* @parm char * | pszBuffer | 
*       Message to display at prompt.
*
* @rdesc 
* @flag int | VS_SUCCESS if the password is correct
* @flag int | VS_ERR if the password is incorrect
* @end
**********************************************************************************************/
int inGetPassword(long lnMessage,char* pszBuffer) {
    int inResult;
    /** pdebug(("--inGetPassword--"));**/
    obDisp.inMinPWLength = 1;
    inResult = inEnterWithMessage(lnMessage, SCROLL_DISPLAY_FALSE, pszBuffer, INPUT_ALNUMS_PWD, PASSWORD_SIZE, inValidatePwdMin);
    return inResult;
}

/********************************************************************************************* 
* @func int | inValidatePwdMin |
*             Validates Minimum password length using Display Object Length Variable
              which must be loaded prior to validation invocation.
*
* @parm char * | szInputPwd | 
*       Input password
*
* @rdesc 
* @flag int | VS_SUCCESS if the password is correct
* @flag int | VS_ERR if the password is incorrect
* @end
**********************************************************************************************/
int inValidatePwdMin(char* szInputPwd) {
    if (strlen(szInputPwd) < obDisp.inMinPWLength) {
        vdSGErrorMessage(INV_PWD_ERR_MSG21);
        return (VS_ERR);
    }

    return(VS_SUCCESS);
}

/********************************************************************************************* 
* @func int | inValidateSuperPwd |
*             Gets a password from the user and validates this password
*             against the one stored in memory.
*
* @parm char * | szInputPwd | 
*       Input password
*
* @rdesc 
* @flag int | VS_SUCCESS if the password is correct
* @flag int | VS_ERR if the password is incorrect
* @end
**********************************************************************************************/
int inValidateSuperPwd(char* szInputPwd) {
    char szSavedPwd[PASSWORD_SIZE + 1];

    /**pdebug(("--inValidateSuperPwd--"));**/

    strcpy(szSavedPwd, szGetSuperPwd());
    /**(("Supv = <%s>", szSavedPwd));**/
    /**(("Entered = <%s>", szInputPwd));**/
    if (strcmp(szSavedPwd, szInputPwd) != 0) {
        if (inValidateManagerPwd(szInputPwd) != VS_SUCCESS) {
            vdSGErrorMessage(INV_PWD_ERR_MSG21);
            return(VS_ERR);
        }
    } else
        vdSetPWSecurityLevel(ACCESS_WITH_SUPER_PASSWORD);

    return(VS_SUCCESS);
}
/********************************************************************************************* 
* @func int | inValidateManagerPwd|
*             Gets a password from the user and validates this password
*             against the one stored in memory.
*
* @parm char * | szInputPwd | 
*       Input password
*
* @rdesc 
* @flag int | VS_SUCCESS if the password is correct
* @flag int | VS_ERR if the password is incorrect
* @end
**********************************************************************************************/

int inValidateManagerPwd(char* szInputPwd) {
    char szSavedPwd[PASSWORD_SIZE + 1];
    char szEnv[10];

    /**(("--inValidateManagerPwd--"));**/

    strcpy(szSavedPwd, szGetManagerPwd());

    /*
     *      Validate MegaPassword...
     */
    if (get_env("MPW", szEnv, 9)) {
             if (strcmp(szInputPwd, "SOFPAY") == 0 || strcmp (szInputPwd, "763729") == 0){
        //if (strcmp(szInputPwd, "123456") == 0 || strcmp(szInputPwd, "123456") == 0) {
            vdSetMegaPassword(VS_TRUE);
            vdSetPWSecurityLevel(ACCESS_WITH_MANAGER_PASSWORD);
            return(VS_SUCCESS);
        }
    }
    vdSetMegaPassword(VS_FALSE);

    /**pdebug(("Manager = <%s>", szSavedPwd));**/
    /**(("Entered = <%s>", szInputPwd));**/
    if (strcmp(szSavedPwd, szInputPwd) != 0) {
        vdSGErrorMessage(INV_PWD_ERR_MSG21);
        return(VS_ERR);
    }

    vdSetPWSecurityLevel(ACCESS_WITH_MANAGER_PASSWORD);
    return(VS_SUCCESS);
}

/********************************************************************************************* 
* @func void |  inCustomAccessFunction |
*
* @parm VOID
*
* @end
**********************************************************************************************/

int inCustomAccessFunction(void) {
    int inRetVal = VS_ERR;
    PFI_VOID pinCustomAccessFunction;

    if ((pinCustomAccessFunction = inGetFunctionPointer(CUSTOM_ACCESS_FUNCTION)) != NULL_PFI)
        inRetVal = pinCustomAccessFunction();

    return (inRetVal);
}

int inDefaultAccessFunction(void) {
    return(VS_SUCCESS);
}

/********************************************************************************************* 
* @func int | inGetPwdFromUser|
*             Gets a password from the user and validates this password 
*                 against clerk, supervisor, and manager password (in that order)
*
* @parm int | inPasswordLevel | 
*       clerk, supervisor, manager or custom password
*
* @rdesc 
* @flag int | VS_SUCCESS if the password is correct
* @flag int | VS_ERR if the password is incorrect
* @end
**********************************************************************************************/

int inGetPwdFromUser(int inPasswordLevel,int inPITIndex) {
    int inResult;
    char szInputPwd[PASSWORD_SIZE + 1];

    /**pdebug(("--inGetPwdFromUser--"));**/
    /**pdebug(("Pwd level: %d", inPasswordLevel));**/

    if (fGetKeyboardLock())
        inPasswordLevel = ACCESS_WITH_SUPER_PASSWORD;
    else {
        if (inPITIndex != NO_PIT_INDEX) {
            if ((inLoadPITRec((inPITIndex - 1))) == VS_SUCCESS)
                inPasswordLevel = inGetPwdLevel();
        }
    } /* use PIT table */

    switch (inPasswordLevel) {
    case ACCESS_WITH_MANAGER_PASSWORD:
        /**pdebug(("ACCESS_WITH_MANAGER_PASSWORD"));**/
        if ((inResult = inCheckPWSecurityLevel(ACCESS_WITH_MANAGER_PASSWORD)) != VS_SUCCESS) {
            inResult = inEnterWithMessage(PASSWD_MSG15, SCROLL_DISPLAY_FALSE, szInputPwd, INPUT_ALNUMS_PWD, PASSWORD_SIZE, inValidateManagerPwd);

            vdClearNonTitleLines();
        }
        if (inResult >= 0)
            inResult = VS_SUCCESS;
        break;

    case ACCESS_WITH_SUPER_PASSWORD:
        /**pdebug(("ACCESS_WITH_SUPER_PASSWORD"));**/
        if ((inResult = inCheckPWSecurityLevel(ACCESS_WITH_SUPER_PASSWORD)) != VS_SUCCESS) {
            inResult = inEnterWithMessage(PASSWD_MSG15, SCROLL_DISPLAY_FALSE, szInputPwd, INPUT_ALNUMS_PWD, PASSWORD_SIZE, inValidateSuperPwd);
            vdClearNonTitleLines();
        }
        if (inResult >= 0)
            inResult = VS_SUCCESS;
        break;

#ifdef CLERK_DEFINED

    case ACCESS_WITH_CLERK_PASSWORD:
        /**pdebug(("ACCESS_WITH_CLERK_PASSWORD"));**/
        if ((inResult = inCheckPWSecurityLevel(ACCESS_WITH_CLERK_PASSWORD)) != VS_SUCCESS) {
            inResult = inEnterWithMessage(PASSWD_MSG15, SCROLL_DISPLAY_FALSE, szInputPwd, INPUT_ALNUMS_PWD, PASSWORD_SIZE, inValidateClerkPwd);
            vdClearNonTitleLines();
        }
        if (inResult >= 0)
            inResult = VS_SUCCESS;
        break;

#endif // CLERK_DEFINED

    case ACCESS_WITH_CUSTOM:
        if ((inResult = inCheckPWSecurityLevel(ACCESS_WITH_CUSTOM)) != VS_SUCCESS) {
            inResult = inCustomAccessFunction();
            vdClearNonTitleLines();
        }
        break;

    default:
        /**pdebug(("ACCESS_FREELY"));**/
        inResult = VS_SUCCESS;
    }

    if (fGetKeyboardLock()) {
        if (inResult == VS_SUCCESS) {
            if (inLoadTCTRec(0) != VS_SUCCESS) {
                vdDisplayMessageFromFile(TCT_ERR_MSG21, ERROR_MSG);
                return(VS_ERROR);
            }
            vdSetKeyboardLock(VS_FALSE);     /* Unlock keyboard */

            if (inSaveTCTRec(0) != VS_SUCCESS) {
                vdDisplayMessageFromFile(TCT_ERR_MSG21, ERROR_MSG);
                return(VS_ERROR);
            }
        } else
            inResult = VS_ESCAPE;
    }

    return(inResult);
}

int PASSWD_CODE_CHECK(char* a,char* b,short* c,char* d,unsigned char* e) {
    GET_VER(a, b, c, d, e);
    return 1;
}

#undef VSPASSWD_C

